package com.zcf.service;

import com.zcf.pojo.Agent;
import com.zcf.pojo.User;
import com.zcf.vo.out.LayuiJson;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zcf.common.json.Body;
import com.zcf.mapper.AgentMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-07-05
 */
@Service
public class AgentServiceImpl  {

	@Autowired
	AgentMapper am;
	/**
	 * 查看代理
	 * @param agent
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public LayuiJson setupagent(Agent agent, int pageNum, int pageSize) {
		
		LayuiJson lj = new LayuiJson();
		List<Agent> list ;
		Map<String, Object> map ;
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		if(agent.getUserid() != null){
			list = am.selectPage(new Page<>(pageNum, pageSize), new EntityWrapper<Agent>().eq("userid", agent.getUserid()));
		}else{
			list = am.selectPage(new Page<>(pageNum, pageSize), new EntityWrapper<Agent>());
		}
		if(list.size() !=0){
			for (Agent agent2 : list) {
				getusers(agent2,lists);
			}
		}else{
			if(agent.getUserid() != null){
				User user = new User();
				user.setId(agent.getUserid().longValue());
				User user2 = user.selectById();
				if(user2!=null){
					map = new HashMap<String, Object>();
					map.put("userName", user2.getWxname());
					map.put("userid", user2.getId());
					map.put("type", 0);
					map.put("createTime", user2.getCreatetime());
					map.put("role", user2.getRole());
					lists.add(map);
				}
			}
			
		}
		if (lists != null) {
			lj.setCode(0);
			lj.setCount(list.size());
			lj.setData(lists);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setCount(list.size());
		lj.setData(null);
		lj.setMsg("暂无数据");
		return lj;

}
	/**
	 * 查询用户添加返回
	 * @param agent2
	 * @param lists
	 * @return
	 */
	private List<Map<String, Object>> getusers (Agent agent2, List<Map<String, Object>> lists){
		Map<String, Object> map = new HashMap<String, Object>();
		if(agent2!=null){
			User user = new User();
			user.setId(agent2.getUserid().longValue());
			User user2 = user.selectById();
			if(user2!=null){
				map.put("userName", user2.getWxname());
			}
			map.put("userid", agent2.getUserid());
			map.put("type", agent2.getType());
			map.put("createTime", agent2.getCreateTime());
			map.put("role", user2.getRole());
			lists.add(map);
		}
		return lists;
		
	}
	
	

}
